--local Util = require("common.Util")

local FamilyExcel = require("excel.Family")
local RoleFFI = require("role.RoleFFI")
local StrategyExcel = FamilyExcel.strategy
local BannerExcel = FamilyExcel.banner
local FamilyWarReward = require("excel.FamilyWar").WarReward[1]
local FamilyWarTequan = require("excel.FamilyWar").WarTequan
local HorseLogic = require("horse.HorseLogic")
local ObjMonster = require("core.ObjMonster")
local Map = require("core.Map")
local Buffer = require("buffer.Buffer")
local ObjHuman = require("core.ObjHuman")
local Copy = require("copy.CopyL")
local CopyDefine = require("copy.Define")
local Config = require("Config")
local BagLogic = require("bag.BagLogic")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local ItemDefine = require("bag.ItemDefine")
local Msg = require("core.Msg")
local TitleLogic = require("title.TitleLogic")




function doCalc(human)
    if human.familyUuid == "" and not Config.IS_MIDDLE then
        return
    end

    local nAddAttrList = {}
    local curlv = 0
    local nCeilLv = (human.db.familyLv or 0) * 2
    for k,v in ipairs(StrategyExcel) do
        curlv = human.db.familyStrategyList[k - 1].strategyLev
        curlv = curlv > nCeilLv and nCeilLv or curlv
        local curstrateAttr = v.strategydata[curlv]
        if curstrateAttr then
            for k,v in ipairs(curstrateAttr[3]) do
                nAddAttrList[v[1]] = (nAddAttrList[v[1]] or 0) + v[2]
            end
        end
    end

    local nBannerLv = human.db.familyBanner
    local nBannerConfigData = BannerExcel[nBannerLv]
    if nBannerConfigData then
        local nAddAttrData = nBannerConfigData.addattr
        if nAddAttrData then
            for k,v  in ipairs(nAddAttrData) do
                nAddAttrList[v[1]] = (nAddAttrList[v[1]] or 0) + v[2]
            end
        end
    end


    for k, v in pairs(nAddAttrList) do
        human.attr[k] = human.attr[k] + v
    end

    return 0
end


function getFamilyTitle(human)
    if not Config.IS_MIDDLE and human.familyUuid == "" then return 0 end
    if human.db.familyWarResult == 0 then return 0 end
    local k = FamilyWarTequan[human.db.familyWarResult]
    if not k then return 0 end
    return k.titleID or 0
end

monster_winner  = monster_winner or nil
function createFamilyWarWinner(data)
    if not data then
        if monster_winner then
         ObjMonster.destroy(monster_winner)
         monster_winner = nil
        end
        return
    end
    
--    if monster_winner and monster_winner.familyWarData.uuid == data.uuid then
--        return 
--    end
    if monster_winner then
         ObjMonster.destroy(monster_winner)
         monster_winner = nil
    end
    monster_winner = ObjMonster.create(3014009)
    monster_winner.familyWarData = data
    monster_winner.name = data.leaderName
    monster_winner.familyName = data.name  
    local scene_id = Map.map_id_to_scene_id[Map.getHomeMapID()][1]
    ObjMonster.enterScene(monster_winner, scene_id, 4849, 2926)
    
    print("leaderName: c",data.leaderName, data.name)
end

function onUserLogin(human) -- 激活或删除坐骑。
    checkFamilyWarResult(human, true)
end

function checkAllUserFamilyWarResult()
    for uuid, human in pairs(ObjHuman.onlineUuid) do
        if human.fd then
            checkFamilyWarResult(human)
        end
    end
end

function getFamilyLimitTime(human)
    local d = os.date("*t",os.time())
    local limitTime  = 0
    if d.wday < 6 then
        d.hour = 23
        d.min = 59
        limitTime = os.time(d) - os.time() + (5 - d.wday)*24*3600
    elseif d.wday == 7 and (d.hour == 20 and d.min > 20 or d.hour > 20) then            
        d.hour = 23
        d.min = 59
        limitTime = os.time(d)+ 5*3600*24 - os.time()            
    end
    return limitTime
end

function checkFamilyWarResult(human, isLogin)
    local data = FamilyWarTequan[human.db.familyWarResult]
    if not data then -- 删加属性的buf
        for k, v in pairs(FamilyWarTequan) do
            Buffer.removeID(v.attr, human, true)
        end
        HorseLogic.clearHuanhuaHorse(human, 34, isLogin)
        
        if not isLogin then
            local oldTitleID = TitleLogic.getTitle(human)
            TitleLogic.onFamilyWarTitle(human)  
            if oldTitleID ~= TitleLogic.getTitle(human) then
                TitleLogic.sendHumanTitle(human)
            end
        end
        return
    end
    
    if data.attr > 0 and not Buffer.hasBuf(human, data.attr) then  --加属性
        Buffer.add(data.attr, human, human)
    end
    if human.db.familyWarResult == 1 and HorseLogic.isGetHuanhuaHorse(human, 34) == 0 then   -- 激活坐骑
        local d = os.date("*t",os.time())
        local limitTime  = 0
        if d.wday < 6 then
            d.hour = 23
            d.min = 59
            limitTime = os.time(d) - os.time() + (5 - d.wday)*24*3600
        elseif d.wday == 7 and (d.hour == 20 and d.min > 20 or d.hour > 20) then            
            d.hour = 23
            d.min = 59
            limitTime = os.time(d)+ 5*3600*24 - os.time()            
        end
        if limitTime > 0 then
            HorseLogic.jihuoLimit(human, 34, limitTime, 0, 0, true,isLogin)   
                
        end
    end      
    if not isLogin then
        local oldTitleID = TitleLogic.getTitle(human)
        TitleLogic.onFamilyWarTitle(human)  
        if oldTitleID ~= TitleLogic.getTitle(human) then
            TitleLogic.sendHumanTitle(human)
        end
    end
end


function getFamilyWarExpAdd(human)
    if not human.scene_id then return 0 end
    local copyID = Copy.isCopyBySceneID(human.scene_id)
    if not copyID then
        return 0
    end
    if CopyDefine.COPY_FAMILY_ONHOOK_ID ~= copyID then
        return 0
    end
    local conf = FamilyWarTequan[human.db.familyWarResult]
    if not conf then
        return 0
    end
    return conf.exp/100    
end


function familyTaoyuanGet(human, index, good)
    if human.db.taoyuanReward[index] ~= 1 then
        return
    end
    local id = ItemDefine.ITEM_ID_BAD_TAO
    if good then
        id = ItemDefine.ITEM_ID_GOOD_TAO
    end
    
    if not BagLogic.addItem(human, id, true, 1, "familytaoyuan") then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.FAMILY_TAOYUAN_BAG_FULL)
        return        
    end
    human.db.taoyuanReward[index] = 2
    local msg = Msg.gc.GC_FAMILY_ACT_TY_REWARD
    msg.index = index
    Msg.send(msg, human.fd)
    Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.FAMILY_TAOYUAN_GET_OK)
    
end
